﻿using System.Linq;
using Enyim.Caching;
using System.Web.Mvc;
using SilenthinkContract;
using Web.Areas.My.Models;
using SilenthinkContract.Domain;
using SilenthinkCommon.Extension;
using SilenthinkCommon.Components;

namespace Web.Areas.My.Controllers
{
    public class OrderController : MyBaseController
    {
        public OrderController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public ActionResult Index(int page = 1, int size = 20)
        {
            var userId = User.Identity.SkIdentity().Id;
            var model = new OrderControllerModel.Index();
            var orders = OrderService.GetPagedOrder(userId: userId).ToPagedList(page, size);
            if (orders.Count > 0)
            {
                var orderIds = orders.Select(o => o.Id).ToArray();
                var orderItems = OrderService.GetOrderItems(orderIds);
                foreach (var order in orders)
                {
                    order.OrderItems = orderItems.Where(o => o.OrderId == order.Id).ToList();
                }
            }
            model.Orders = orders;
            return View(model);
        }

       public ActionResult Auction(int page = 1, int size = 20)
        {
            var identity = User.Identity.SkIdentity();
            var model = new OrderControllerModel.Auction();
            model.Orders = AuctionService.GetPagedAuctionOrder(page, size, identity.Id).ToPagedList(page, size);
            return View(model);
        }

        /// <summary>
        /// 订单详细信息
        /// </summary>
        /// <param name="param">订单号</param>
        /// <returns></returns>
        public ActionResult Detail(string param)
        {
            var userId = User.Identity.SkIdentity().Id;
            var model = new OrderControllerModel.Detail();
            model.Order = OrderService.GetOrder(param, userId, true);
            return PartialView(model);
        }
        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="param">订单号</param>
        /// <returns></returns>
        public ActionResult Edit(string param)
        {
            var userId = User.Identity.SkIdentity().Id;
            var model = new OrderControllerModel.Detail();
            model.Order = OrderService.GetOrder(param, userId, true);
            return View(model);
        }

        /// <summary>
        /// 订单支付 检查订单状态等
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ActionResult Pay(string param)
        {
            var url = string.Empty;
            var identity = User.Identity;
            var user = identity.SkIdentity();
            var order = OrderService.GetOrder(param, user.Id);
            if (order != null)
            {
                var trade = PaymentService.GetUserTrade(param, user.Id);
                if (trade != null)
                {
                    if (trade.Status != UserTradeStatus.Pending && order.OrderStatus != OrderStatus.Pending)
                    {
                        url = "/order/detail/" + param;
                    }
                    else
                    {
                        url = WebSite.Pay + "/order?param=" + param;
                    }
                }
                else
                {
                    PaymentService.CreateUserTrade(order.ToTrade());
                    url = WebSite.Pay + "/order?param=" + param;
                }
            }
            return Redirect(url);
        }
    }
}
